package com.shuyu.web.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shuyu.common.core.controller.BaseController;
import com.shuyu.common.core.domain.Result;
import com.shuyu.common.core.page.TableDataInfo;
import com.shuyu.common.utils.SecurityUtils;
import com.shuyu.domain.Message;
import com.shuyu.domain.MessageHistory;
import com.shuyu.service.MessageHistoryService;
import com.shuyu.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 授权登录相关
 * 
 * @author
 */
@RestController
@RequestMapping("/api/message")
@Api(tags = "前端-用户留言")
public class MessageController extends BaseController
{
    @Resource
    private MessageService messageService;
    @Resource
    private MessageHistoryService messageHistoryService;

    @PostMapping
    @ApiOperation("提交用户留言信息--APP")
    public Result add(@RequestBody Message message){
        Long userId = SecurityUtils.getLoginUser().getUserId();
        Date date = new Date();
        message.setCreateTime(date);
        message.setStatus(0);
        message.setCustomerId(userId);
        message.setReplyType("初次回复");
        message.setFinishStatus(0);
        messageService.save(message);
        MessageHistory messageHistory = new MessageHistory(message.getId(), message.getMessage(), date, 0, userId);
        return Result.success(messageHistoryService.save(messageHistory));
    }

    @GetMapping
    @ApiOperation("获取用户留言列表--APP")
    public TableDataInfo<List<Message>> list(){
        startPage();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<Message> list = messageService.list(new LambdaQueryWrapper<Message>()
                .eq(Message::getCustomerId, userId).eq(Message::getDelFlag,0)
                .orderByAsc(Message::getFinishStatus).orderByDesc(Message::getReplyTime,Message::getCreateTime));
        return getDataTable(list);
    }

    @GetMapping("/{messageId}")
    @ApiOperation("获取用户某条留言详情--APP")
    public TableDataInfo<List<Message>> detail(@PathVariable int messageId){
        List<MessageHistory> list = messageHistoryService.list(new LambdaQueryWrapper<MessageHistory>().eq(MessageHistory::getMessageId, messageId)
                .orderByDesc(MessageHistory::getCreateTime));
        Message message = new Message();
        message.setReadStatus(1);
        message.setId(messageId);
        message.setReadTime(new Date());
        messageService.updateById(message);
        return getDataTable(list);
    }
    @PostMapping("/append")
    @ApiOperation("用户追加留言--APP")
    public Result append(@RequestBody Message message){
        Long userId = SecurityUtils.getLoginUser().getUserId();
        Date date = new Date();
        message.setCreateTime(date);
        message.setStatus(0);
        message.setCustomerId(userId);
        message.setReplyType("追加回复");
        if(message.getScore()<5){
            message.setFinishStatus(0);
        }else {
            message.setFinishStatus(1);
        }
        messageService.updateById(message);
        MessageHistory messageHistory = new MessageHistory(message.getId(), message.getMessage(), date, 0, userId);
        return Result.success(messageHistoryService.save(messageHistory));
    }
    @DeleteMapping("/delReadMessage")
    @ApiOperation("删除已读消息--APP")
    public Result delReadMessage(){
        messageService.update(new LambdaUpdateWrapper<Message>()
                .set(Message::getDelFlag,1).set(Message::getDelTime, new Date()).eq(Message::getReadStatus,1));
        return Result.success();
    }
    @PutMapping("/readMessageAll")
    @ApiOperation("全部已读消息--APP")
    public Result readMessageAll(){
        messageService.update(new LambdaUpdateWrapper<Message>()
                .set(Message::getReadStatus,1).set(Message::getReadTime, new Date())
                .eq(Message::getReadStatus,0).eq(Message::getDelFlag,0));
        return Result.success();
    }

}
